package com.chenxbook.modules.jbpm4.tool;

import lombok.Data;
import org.jbpm.api.IdentityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Arrays;

/**
 * <p>className: JbpmTaskTool</p>
 * <p>description: 自定义工作流身份认证工具类</p>
 *
 * @author chenxbook
 * @version 1.0.0
 * @date 2020-05-23 19:48
 */
@Data
@Component("jbpmIdentityTool")
public class JbpmIdentityTool {

    private static final Logger log = LoggerFactory.getLogger(JbpmIdentityTool.class);

    /**
     * IdentityService-身份认证服务接口
     * <br> 提供对流程用户、用户组以及组成员关系的相关服务
     */
    @Autowired
    @Qualifier(value = "identityService")
    private IdentityService identityService;

    /***
     * 创建用户组
     *
     * @param groupName 组名
     * @author
     * @date 2020/10/31 16:04
     * @return
     */
    public String createGroup(String groupName){
        log.info("parameters，groupName: {} ", groupName);
        String groupId = identityService.createGroup(groupName);
        log.info("groupId: {} ", groupId);
        return groupId;
    }


    /***
     * 创建用户
     *
     * @param userId 用户ID
     * @param givenName 名字
     * @param familyName 姓氏
     * @author chenxbook
     * @date  2020/10/31 16:04
     * @return
     */
    public void createUser(String userId, String givenName, String familyName){
        String message = MessageFormat.format("userId=[{0}],givenName=[{1}],familyName=[{2}]", userId, givenName, familyName);
        log.info("parameters，{} ", message);
        identityService.createUser(userId,givenName,familyName);
    }


    /***
     * 将用户加入用户组
     *
     * @param groupId  组ID
     * @param userIds 用户ID(多个用户ID之间以逗号分隔）
     * @author chenxbook
     * @date 2020/10/31 16:04
     * @return
     */
    public void createMembership(String groupId,String ...userIds){
        log.info("parameters，groupId=[{}],userIds=[{}] ", groupId,userIds);
        Arrays.stream(userIds).forEach(userId->{
            identityService.createMembership(userId,groupId);
        });
    }











}